package com.fancy.learncenter.net;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;

import com.fancy.learncenter.activity.LoginActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.CustomProgressDialog;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by hyy on 2017/12/1.
 * describe as
 */

public abstract class IdeaObserver<T extends BaseDataBean> implements Observer<BaseDataBean<T>> {

    public static int SUCCESS_CODE = 200;
    public static int LOGIN_CODE = 10004;
    public static int LOGIN_CODE2 = 10001;
    public static int SYSTEM_ERROR_CODE = 500;
    Dialog dialog;

    Activity activity;

    //    自定义界面，不拉登陆接口
    boolean isLoginCallBack = false;

    public IdeaObserver() {

    }

    public IdeaObserver(Activity activity, boolean isShowDialog) {

        this.activity = activity;
        if (isShowDialog) {
            dialog = CustomProgressDialog.creatRequestDialog(activity);
        }
    }

    public IdeaObserver(Activity activity, boolean isShowDialog, boolean isLoginCallBack) {

        this.activity = activity;
        this.isLoginCallBack = isLoginCallBack;
        if (isShowDialog) {
            dialog = CustomProgressDialog.creatRequestDialog(activity);
        }
    }

    public IdeaObserver(Activity activity) {
        this.activity = activity;
    }


    @Override
    public void onNext(@NonNull BaseDataBean<T> tBaseDataBean) {
        if (tBaseDataBean.getStatus() == SUCCESS_CODE) {
            onSuccess((T) tBaseDataBean);
        } else if (tBaseDataBean.getStatus() == LOGIN_CODE || tBaseDataBean.getStatus() == LOGIN_CODE2) {
            if (activity != null) {
                Intent intent = new Intent(activity, LoginActivity.class);
                activity.startActivity(intent);
                activity.finish();
            }
        } else {

            //弹出服务器返回的错误信息
            if (tBaseDataBean != null && !TextUtils.isEmpty(tBaseDataBean.getMsg())) {
                ToastUtil.show(tBaseDataBean.getMsg());
                onFail(tBaseDataBean.getStatus());
            }

        }
    }

    @Override
    public void onComplete() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }

        onFail(SYSTEM_ERROR_CODE);


        LogUtil.MyLog("onError", "=======" + e);
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {

        if (dialog != null) {
            dialog.show();
        }
    }

    public abstract void onSuccess(T t);

    public abstract void onFail(int errorCode);


}